<template>
  <div>
    <el-card>
      <el-row type="flex">
        <el-col>
          <el-button size="small" type="primary" @click="isShowDialog = true">添加页面</el-button>
        </el-col>
        <el-col>
          <el-row type="flex">
            <el-input v-model="searchOption.title" placeholder="请输入关键字" />
            <el-button @click="search">
              筛选
            </el-button>
          </el-row>
        </el-col>
      </el-row>
      <el-divider />
      <el-table
        :data="pages"
        border
      >

        <el-table-column label="标题" prop="title" />
        <el-table-column label="内容">
          <template #default="{row}">
            <div style="max-height:180px;overflow: auto;" v-html="row.content" />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button size="mini" type="primary" @click="updatePage(row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="delPage(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-divider />
      <el-row type="flex" justify="center">
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          :page-size="pagination._limit"
          @current-change="currentChange"
        />
      </el-row>
    </el-card>
    <el-dialog :title="`${data.id?'编辑':'添加'}页面`" :visible="isShowDialog" destroy-on-close @close="btnClose">
      <el-form ref="form" :model="data" :rules="rules">
        <el-form-item label-width="68px" label="标题" prop="title">
          <el-input v-model="data.title" />
        </el-form-item>
        <el-form-item label-width="68px" label="内容" prop="content">
          <VueEditor v-model="data.content" />
        </el-form-item>

      </el-form>
      <template #footer>
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnClose">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { VueEditor } from 'vue2-editor'
import { addPage, delPage, getPages, getPagesCount, getPageDetail, updatePage } from '@/api/page'
import { getAgents } from '@/api/agent'
export default {
  components: { VueEditor },
  data() {
    return {
      searchOption: {
        title: '',
        agent: ''
      },
      searchOptionConfirmed: {
        title: '',
        agent: ''
      },
      pages: [],
      pagination: {
        _limit: 2,
        _start: 0
      },
      agents: [],
      total: 0,
      isShowDialog: false,
      data: {
        title: '',
        content: ''
      },
      rules: {
        title: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    searchOption: {
      deep: true,
      immediate: true,
      handler() {
        this.searchOptionConfirmed = {}
        for (const key in this.searchOption) {
          if (this.searchOption[key]) {
            this.searchOptionConfirmed[`${key}_contains`] = this.searchOption[key]
          }
        }
      }
    }
  },
  created() {
    this.getPages()
  },
  methods: {
    async getAgents() {
      this.agents = await getAgents({
        _limit: -1
      })
    },
    async getPages() {
      this.pages = await getPages({
        ...this.pagination,
        ...this.searchOptionConfirmed
      })
      this.total = await getPagesCount({
        ...this.pagination,
        ...this.searchOptionConfirmed
      })
    },
    currentChange(newPage) {
      this.pagination._start = (newPage - 1) * this.pagination._limit
      this.getPages()
    },
    async delPage(id) {

      await delPage(id)
      this.$message.success('删除成功')
      this.getPages()
    },
    async updatePage(id) {
      const res = await getPageDetail(id)
      this.data = {
        ...res,
        agent: res.agent?.id
      }
      this.isShowDialog = true
    },
    async btnOK() {
      await this.$refs.form.validate()
      if (this.data.id) {
        await updatePage(this.data)
      } else {
        await addPage(this.data)
      }
      this.$message.success('操作成功')
      this.isShowDialog = false
      this.getPages()
    },
    btnClose() {
      this.data = {}
      this.isShowDialog = false
    },
    search() {
      this.getPages()
    }
  }
}
</script>

<style>

</style>
